// packagePersonnel/pages/recruitment-list/index.js
import Config from "../../../main.config";
import {getStorageSync} from "../../../utils/Storage";
import {getPublisherList, getRecruitmentList, getPublishPositionList} from "../../../service/Position";
import Notification from "../../../utils/Notification"
Page({
    /**
     * 组件的初始数据
     */
    data: {
        //职位
        position: {
            list: [
                { text: "职位类型", value: 0 }
            ],
            current: 0
        },
        //发布人
        publisher: {
            list: [
                { text: "发布人", value: 0 },
                {
                    text: "本人发布",
                    value: 1,
                    search: getStorageSync(Config.storage.userInfo + ".userId")
                }
            ],
            current: 1
        },
        //招聘信息
        recruitment: {
            loading: false,
            reset: false,
            error: false,
            isJoinCompany: true,
            total: -1,
            list: []
        },
    },

    onLoad: function () {
        let registerInfo = getStorageSync(Config.storage.registerInfo);
        let userId = getStorageSync(Config.storage.userInfo + ".userId");
        let companyId = registerInfo.companyId;
        let status = registerInfo.status;
        this.data._search = {companyId, createUser: userId};
        this.data._companyId = companyId;
        if (!companyId || status !== 2) { // 未加入公司 或者 没有通过审核
            this.setData({
                "recruitment.isJoinCompany": false
            })
            return false
        }
        this.getPositionTypes();
        this.getPublishers();
        this.setData({"recruitment.loading": true})
    },


    /*
    * 获取职位类型
    * */
    getPositionTypes: async function () {
        try {
            let companyId = this.data._companyId;
            let res = await getPublishPositionList({companyId: companyId});
            let list = this.data.position.list;
            if (res.length === 0) return false;
            let newList = res.map(function (item, index) {
                return {
                    text: item.name,
                    value: index + 1,
                    search: item.name
                }
            })
            this.setData({
                "position.list": list.concat(newList)
            })
        } catch (e) {
            console.error(e)
        }
    },

    /*
    * 获取发布人列表
    * */
    getPublishers: async function () {
        try {
            let res = await getPublisherList();
            let list = this.data.publisher.list;
            if (res.length === 0) return false;
            let newList = res.map(function (item, index) {
                return {
                    text: item.positionName,
                    value: index + 2,
                    search: item.id
                }
            })
            this.setData({
                "publisher.list": list.concat(newList)
            })
        } catch (e) {
            console.error(e)
        }
    },

    /*
    * 列表加载
    * */
    onLoadList: async function (e) {
        try {
            let search = this.data._search;
            let paging = e.detail.paging;
            let list = this.data.recruitment.list;
            let isReset = this.data.recruitment.reset;
            let postData = Object.assign(search, {
                page: paging.page,
                limit: paging.pageSize,
            })
            let res = await getRecruitmentList(postData);
            let newList = res.data;
            list = isReset ? newList : list.concat(newList);
            this.setData({
                "recruitment.list": list,
                "recruitment.total": res.count
            });
        } catch (e) {
            this.setData({
                "recruitment.error": true
            })
        } finally {
            this.setData({
                "recruitment.reset": false,
                "recruitment.loading": false
            })
        }
    },

    /*
    * 监听menu切换
    * */
    onMenuChange: function (e) {
        let name = e.currentTarget.dataset.name;
        let index = e.detail;
        let current = this.data[name].current;
        if (index === current) return false;
        let search = this.data._search || {};
        let list = this.data[name].list;
        let value = list[index].search;
        switch (name) {
            case "position":
                value ? search.position = value : delete search.position;
                break
            case "publisher":
                value ? search.createUser = value : delete search.createUser;
                break
            default:
                break
        }
        this.setData({
            [name + ".current"]: index,
            "recruitment.reset": true
        })
    },

    /*
    * 跳转查看详情
    * */
    bindNavigatorToDetail: function (e) {
        const that = this;
        let id = e.currentTarget.dataset.id;
        Notification.remove({
            name: Config.notification.deleteRecruitment,
            observer: that
        })
        Notification.addOnce({
            name: Config.notification.deleteRecruitment,
            callback: function () {
                that.setData({"recruitment.reset": true})
            },
            observer: that
        })
        wx.navigateTo({url: "/pages/common/recruitment-intro/index?id=" + id})
    },

    /*
    * 跳转 发布职位
    * */
    bindToRelease: function () {
        const that = this;
        wx.navigateTo({
            url: "/pages/common/recruitment-release/index",
            events: {
                // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
                acceptDataFromOpenedPage: function(data) {
                    data.isChange ? that.setData({
                        "recruitment.reset": true
                    }) : null
                }
            },
        })
    },

    /*
    * 触底加载更多
    * */
    onReachBottom: function () {
        this.setData({"recruitment.loading": true})
    }
})